package cn.cjlmonster.demo02.decoration;

import android.content.Context;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import cn.cjlmonster.demo02.utils.DpUtils;

/**
 * This project is Demo02
 * Created by cjl on 2021/11/7 5:25 下午.
 */

public class GalleryItemDecoration  extends RecyclerView.ItemDecoration {

    int mPageMargin;
    int mLeftPageVisibleWidth;
    int itemHeight;

    public GalleryItemDecoration(Context context) {
        mPageMargin = DpUtils.dpToPx(context, 15);
        int itemWidth = DpUtils.dpToPx(context, 200);
        itemHeight = DpUtils.dpToPx(context, 300);
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        int width = metrics.widthPixels;
        mLeftPageVisibleWidth = (width - itemWidth) / 2;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int position = parent.getChildAdapterPosition(view);
        int itemCount = parent.getAdapter().getItemCount();
        int leftMargin;
        int topAndBottomMargin = (parent.getHeight() - itemHeight) / 2;
        if (position == 0) {
            leftMargin = mLeftPageVisibleWidth;
        } else {
            leftMargin = mPageMargin;
        }
        int rightMargin;
        if (position == itemCount - 1) {
            rightMargin = mLeftPageVisibleWidth;
        } else {
            rightMargin = mPageMargin;
        }
//        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();
//        params.leftMargin = leftMargin;
//        params.rightMargin = rightMargin;
//        view.setLayoutParams(params);
        outRect.set(leftMargin, topAndBottomMargin, rightMargin, topAndBottomMargin);
        
    }
}
